package com.sk.sourcecircle.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;

import com.jude.rollviewpager.RollPagerView;
import com.sk.sourcecircle.R;


/**
 * Created by Administrator on 2017/11/2.
 */

public class FixedProportionViewPager extends RollPagerView {


    private int radioHeight = 1;
    private int radiowidth = 1;
    private int width = 0;
    private int height = 0;
    private float radio = 1;
    private int realHeight = 0;
    private int realWidth = 0;


    public FixedProportionViewPager(Context context) {
        this(context, null);
    }

    public FixedProportionViewPager(Context context, AttributeSet attrs) {
        super(context,attrs);
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FixedProportionViewPager, 0, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.FixedProportionViewPager_radioWidth:
                    radiowidth = array.getInteger(attr, 1);
                    break;
                case R.styleable.FixedProportionViewPager_radioHeight:
                    radioHeight = array.getInteger(attr, 1);
            }
        }

        radio = radiowidth / (float) radioHeight;

        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        width = dm.widthPixels;
        height = dm.heightPixels;
        realWidth = width;
        realHeight = (int) (realWidth / radio);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int newwidthMeasureSpec = MeasureSpec.makeMeasureSpec(realWidth, MeasureSpec.EXACTLY);
        int newheightMeasureSpec = MeasureSpec.makeMeasureSpec(realHeight, MeasureSpec.EXACTLY);
        super.onMeasure(newwidthMeasureSpec, newheightMeasureSpec);
    }
}
